﻿using Microsoft.Scripting.Hosting;
using PythonHost = IronPython.Hosting.Python;

namespace XEnamel.Python
{
	public class PythonContext
	{
		#region Constructors

		public PythonContext()
		{
			var setup = PythonHost.CreateRuntimeSetup(null);
			var runtime = new ScriptRuntime(setup);
			Engine = PythonHost.GetEngine(runtime);
			Scope = Engine.CreateScope();
		}

		#endregion

		#region Properties

		public ScriptEngine Engine { get; private set; }

		public ScriptScope Scope { get; private set; }

		#endregion

		#region Methods

		public dynamic GetVariable(string name)
		{
			return Scope.GetVariable(name);
		}

		public T GetVariable<T>(string name)
		{
			return Scope.GetVariable<T>(name);
		}

		public dynamic ExecuteSource(string expression)
		{
			var source = Engine.CreateScriptSourceFromString(expression);
			return source.Execute(Scope);
		}

		#endregion
	}
}